package assurbike;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;

public class BootStrap
{
	public static void main(String[] args)
	{

//		 ApiAssurBike.init();

		Interface in = new Interface();

//		 try
//		 {
//		 automatic();
//		 }
//		 catch (IOException e)
//		 {
//		 e.printStackTrace();
//		 }
//		 catch (InterruptedException e)
//		 {
//		 e.printStackTrace();
//		 }

		//

	}

	private static String fileAuto = "C:/opt/test.txt";
	
	public static void automatic() throws IOException, InterruptedException
	{
		File fileCre = new File(fileAuto);
		if (!fileCre.exists())
		{
			fileCre.createNewFile();
		}
		
		Map<String, Integer> marques = DataAssurance.getListeMarquesSelection();
		FileWriter file = new FileWriter(fileAuto, true);
		BufferedWriter output = new BufferedWriter(file);
		for (Entry<String, Integer> entryMarque : marques.entrySet())
		{
			String marqueName = entryMarque.getKey();
			int marqueId = entryMarque.getValue();
			Map<String, String> cylindreeList = ApiAssurBike
					.getCylindreesMap(marqueId);
			for (Entry<String, String> entryCylindree : cylindreeList
					.entrySet())
			{
				String cylindreeName = entryCylindree.getKey();
				String cylindreeId = entryCylindree.getValue();
				int cylndreeNameInt = Integer.parseInt(cylindreeName.trim());
				if (cylndreeNameInt < 600)
					continue;
				Map<String, String> typesList = ApiAssurBike
						.getTypesMap(cylindreeId);
				for (Entry<String, String> entryTypes : typesList.entrySet())
				{
					String typeName = entryTypes.getKey();
					String typeId = entryTypes.getValue();

					ApiAssurBike.etape1(marqueId, cylindreeId, typeId, null, null);
					ApiAssurBike.etape2();
					ApiAssurBike.etape3();
					String calcul = ApiAssurBike.calculate();
					calcul = calcul.substring(30);
					calcul = calcul.substring(0, calcul.length() - 57);
					StringTokenizer tokenizer = new StringTokenizer(calcul, ",");
					int index = 0;
					while (tokenizer.hasMoreTokens())
					{
						String value = tokenizer.nextToken();
						if (index == 0 || index == 3 || index == 6
								|| index == 9)
							value = value.substring(1);
						if (index == 2 || index == 5 || index == 8
								|| index == 11)
							value = value.substring(0, value.length() - 1);
						String ligne = marqueName + ";" + typeName + ";"
								+ cylindreeName + ";" + index + ";" + value
								+ "\n";
						output.append(ligne);
						System.out.println(ligne);
						Thread.sleep(500);
						index++;
					}
				}
			}

		}
		output.flush();
		output.close();
	}

}
